<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/16
 * Time: 14:02
 */
namespace app\api\model;
use think\Controller;
use think\Model;
use think\Db;
class Method extends Model
{
    /*
     * 首页 精选攻略
     */
    public function selected($page)
    {
        return Db::name('method')->alias('a')->field('a.title,a.mid,a.title,a.content,a.image,u.user_icon,u.nickname')
            ->join('user u', 'a.uid=u.uid', 'left')
            ->where('a.is_pass', '1')
            ->where('a.is_del', '0')
            ->where('a.is_select', '1')
            ->order('a.create_time', 'desc')
            ->limit(($page - 1) * config('page_size'), config('page_size'))
            ->select();
    }

    /*
     * 攻略列表
     * field 标题，内容，是否有图片type，点赞数，评论数，转发数，头像，昵称，发布距今时间
     */
    public function method_list($where, $page,$limit='',$by='')
    {
         
        $desc = array('1'=>'create_time','2'=>'a.like_num'); 
        // $desc =  ['create_time', 'a.like_num'];
        $limit = ($limit==='')?config('page_size'):$limit;  
        $res =  Db::name('method')
            ->alias('a')
            ->field('a.mid as id,a.uid,a.title,mtext as content,a.image,a.title,u.user_icon,u.nickname,a.create_time,like_num+set_like_num  like_num,share_num+set_share_num  share_num')
            ->join('user u', 'a.uid=u.uid', 'left')
            ->where(['a.is_pass'=>'1','a.is_del'=>0,'a.is_display'=>0]) 
            ->where($where)
            ->order($desc[$by],'desc')
            ->limit(($page - 1) * $limit, $limit)
            ->select();  
            //评论数
        if(count($res)>0)
        { 
            foreach ($res as $key => $value) { 
                  $res[$key]['image'] = [];
                $res[$key]['status'] = 1;
                $res[$key]['type'] = 1;
                //评论数
                $res[$key]['comment_num'] =  Db::name('comment_method')
                ->where(['is_display'=>0,'is_del'=>0,'pid'=>0,'wid'=>$value['id']])
                ->count(); 
                $res[$key]['create_time'] = format_date(strtotime($value['create_time']));
                if(!empty($value['image']))
                $res[$key]['image'] = array_slice(explode(',', $value['image']),0,3);  
                if(count($res[$key]['image'])>0){
                    foreach ($res[$key]['image'] as $keys => $values) {
                        $res[$key]['image'][$keys] = addWaterByField($values);
                    }
                }
            }
            return $res;
        }
        return $res;    
    }

    /*
     * 获取攻略的总个数
     */
    public function method_sum()
    {
        return  Db::name('method')->where('is_pass',1)->where('is_del',0)->count();
    }
    /*
     * 获取攻略的总阅读数
     */
    public function read_all()
    {
        return Db::name('method')->field('read_num')->where('is_pass',1)->where('is_del',0)->sum('read_num');
    }
    /**
     * [getMethodInfo 根据mid获取攻略详情]
     * @Author   kendy
     * @DateTime 2019-10-24T11:40:10+0800
     * @param    [type]                   $mid [攻略id]
     * @return   [type]                        [description]
     */
    public function getMethodInfo($mid,$uid)
    {   
        //浏览量加1
        Db::name('method')->where(['mid'=>$mid,'is_del'=>0])->setInc('read_num',1); 
        $resultData = Db::name('method')
        ->alias('m') //点赞，收藏
        ->field('m.title,m.content,m.image,u.user_icon,u.nickname,m.create_time,m.view_id,share_num,like_num,like_num+set_like_num as like_num, collect_num+set_collect_num as collect_num')
        ->join('user u','u.uid = m.uid','left')
        ->where(['m.is_del'=>0,'mid'=>$mid])
        ->find();
        if(!empty($resultData))
        { 
           $resultData['image'] = addWaterByField($resultData['image']);
           $resultData['create_time'] = format_date(strtotime($resultData['create_time']));
           //评论数 
           $resultData['comment_num'] = Db::name('comment_method')
           ->where(['is_display'=>0,'is_del'=>0,'pid'=>0,'wid'=>$mid])
           ->count();
           //获取景点id和景点名称
           $tourIdArr = explode(',', $resultData['view_id']);
           $where['tour_id'] = ['IN',$tourIdArr];
           $where['is_pass'] = 1;
           $where['is_del'] = 0;
           $resultData['tour'] = Db::name('tours')->field('tour_id,tour_name,tour_covers')->where($where)->select(); 
           if(count($resultData['tour'])>0){
               foreach ($resultData['tour'] as $k => $v) {
                    $resultData['tour'][$k]['tour_covers'] = addWaterByField($v['tour_covers']);
               }
           }

           unset($resultData['view_id']);
          //查看是否点赞和收藏此攻略
          $resultData['like_status'] = Db::name('log_like')
          ->where(['type'=>1,'object_id'=>$mid,'uid'=>$uid])
          ->count();
           if($resultData['like_status']>0)
            $resultData['like_status'] = 1; 
           $resultData['collect_status']=Db::name('collect')
           ->where(['type'=>5,'other_id'=>$mid,'uid'=>$uid])
           ->count();
           if($resultData['collect_status']>0)
            $resultData['collect_status'] = 1;

          // $resultData['recommend'] = $this->method_list('', 1,4); 
           return $resultData;
        }
        return $resultData;
    }
   /**
    * [addMethod 添加攻略]
    * @Author   kendy
    * @DateTime 2019-10-28T15:09:43+0800
    * @param    [type]                   $data [description]
    */
    public function addMethod($data)
    {  

        $user = new Method();
        $user->data($data);
        $user->save(); 
    }

    /*
     * 我的攻略
     */
    public function myMethod($uid,$page)
    {
        $res = Db::name('method')->alias('a')->field('a.title,a.mid,a.title,a.content,a.image,u.user_icon,u.nickname')
            ->join('user u','u.uid=a.uid','left')
            ->where('a.is_pass', '1')
            ->where('a.is_del', '0')
            ->where('a.uid',$uid)
            ->order('a.create_time', 'desc')
            ->limit(($page - 1) * config('page_size'), config('page_size'))
            ->select();
        //处理封面

        foreach($res as $k=>$v){
            $image = explode(',',$v['image']);
            $res[$k]['covor'] = addWaterByField($image[0]);
            unset($res[$k]['image']);
        }

        return $res;
    }
}